<?php
/**
 * news controller
 */
namespace App\Http\ControllersAdmin;

use App\Service\NewsOrigService;
use App\Service\RolesService;
use App\Validation\NewsOrgValidation;
use App\Validation\NewsSearchValidation;
use App\Validation\DeleteValidation;
use App\Validation\BatchValidation;
use App\Models\EwNewsOrig;
use App\Models\EwPublish;

class NewsOrgController extends BaseController
{
	const DISPLAY_TRUE = 1;		//显示标识：是
	const DISPLAY_FALSE = 0;	//显示标识：否
	const COLLECT_TRUE = 1;		//收藏标识：是
	const COLLECT_FALSE = 0;	//收藏标识：否

	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$user = $this->getUser();
		$raw = $this->validation(new NewsSearchValidation, $this->request->getJsonRawBody());
		//body 传入city_id 2017-11-08 +
		$city_id = property_exists($raw,'city_id') ? $raw->city_id : 0;

		//2017-11-28 +
		if ($user->role_id != 2)
			$raw->creator_id = $user->member_id;

		$d = [];
        $this->backgroudDateFilterByCityRole($raw);

        //如果是超级管理员 city_id = body 传入city_id 否者为编辑所在城市的city_id 2017-11-08 +
        $raw->city_id = (property_exists($raw,'city_id') && $raw->city_id == 0) ? $city_id : $raw->city_id;

		if (isset($raw->target_id) && $raw->target_id){
			$data = NewsOrigService::getRecoList($raw);
		}else{
			$data = NewsOrigService::getSearch($raw);
		}
		
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id;
			if ($value->topic_sign == 1)
				$d[$key]->go_url = $this->config->gourl . $this->config->urls->news_topic . $value->id;

			$d[$key]->pics = isset($value->pics) ? explode(',', $value->pics) : [];
			$d[$key]->pics = array_filter($d[$key]->pics);
		}
		
		$count = NewsOrigService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];

		return $this->echoJson($assign);
	}

	/**
	 * [recoAction 最新推荐列表]
	 * @return [type] [description]
	 */
	public function recoAction()
	{
		$this->getUser();
		$raw = $this->validation(new NewsSearchValidation, $this->request->getJsonRawBody());
		// $raw->city_id = $this->city_id;
		$raw->reco_by = 2;
		$d = [];
        $this->backgroudDateFilterByCityRole($raw);
		$data = NewsOrigService::getSearch($raw);
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id;
			$d[$key]->pics = explode(',', $value->pics);
			$d[$key]->pics = array_filter($d[$key]->pics);
		}		
		$count = NewsOrigService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [firstAction 首页列表]
	 * @return [type] [description]
	 */
	public function firstAction()
	{
		$this->getUser();
		$raw = $this->validation(new NewsSearchValidation, $this->request->getJsonRawBody());
		// $raw->city_id = $this->city_id;
		$raw->index_by = 2;
		$d = [];
		$data = NewsOrigService::getSearch($raw);
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id;
			$d[$key]->pics = explode(',', $value->pics);
			$d[$key]->pics = array_filter($d[$key]->pics);
		}		
		$count = NewsOrigService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [publish_task 定时发布加入到队列中]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	
	/**
	 * [createAction 创建]
	 * @return [type] [description]
	 */
	public function createAction()
	{
		$user = $this->getUser();
		$raw = $this->validation(new NewsOrgValidation, $this->request->getJsonRawBody());
		$raw->city_id = intval(RolesService::getCityId($user->role_id));
		$raw->creator_id = $user->member_id;
        if(isset($raw->id) and $raw->id!=''){
            return $this->updateAction();
        }
        $raw->id = NULL;
		if (isset($raw->pics) && is_array($raw->pics) && count($raw->pics) > 0){
			$raw->pics = implode(',', $raw->pics);
		}else{
			$raw->pics = "";
		}

		if (isset($raw->reco_ids) && is_array($raw->reco_ids) && count($raw->reco_ids) > 0){
			$raw->reco_ids = implode(',', $raw->reco_ids);
		}else{
			$raw->reco_ids = "";
		}

		$data = NewsOrigService::save($raw);

		//如果设置了发布时间  2017-11-10 +
		if ($raw->publish_time && $raw->is_display == -1){
			$table_id = NewsOrigService::getTableId();
			$list_id = $data->id;
			$publish_time = $raw->publish_time;
			self::createPublish($table_id, $list_id, $publish_time);
			$delay = strtotime($publish_time) - time();
			$this->queue->put(json_encode([
				"command" => "PUBLISH",
				'table_id' => $table_id,
				'list_id' => $list_id,
				'publish_time' => $publish_time,
				'delay' => $delay
			]));//2017-11-13 +
		}

        //判断是专题那么需要修改其第一个专题的topic_id
        if(isset($raw->topic_id) && $raw->topic_id>0){
            $dataTopic = EwNewsOrig::findFirst($raw->topic_id);
            $dataTopic->topic_id=$raw->topic_id;
            $dataTopic->save();
            //NewsOrigService::save($orgtitle);
        }
        $this->addOperateLogs(array('action'=>'/news-org/create','content'=>'新增原创新闻>'.$raw->title));
		return $this->echoJson('ok');
	}


	public static function createPublish($table_id, $list_id, $publish_time)
	{
		$publish = EwPublish::findFirst('table_id =' . $table_id . ' AND list_id =' . $list_id);
		if (!$publish)
			$publish = new EwPublish;

		$publish->table_id =$table_id;
		$publish->list_id = $list_id;
		$publish->publish_time = $publish_time;
		$publish->is_del = 0;
		$publish->save();
	}

	/**
	 * [ctopicAction 创建]
	 * @return [type] [description]
	 */
	public function ctopicAction()
	{
		$user = $this->getUser();
		$raw = $this->validation(new NewsOrgValidation, $this->request->getJsonRawBody());
		$raw->city_id = intval(RolesService::getCityId($user->role_id));
		$raw->creator_id = $user->member_id;
        if(isset($raw->id) and $raw->id!=''){
            return $this->updateAction();
        }
        $raw->id = NULL;
		if (isset($raw->pics) && is_array($raw->pics) && count($raw->pics) > 0){
			$raw->pics = implode(',', $raw->pics);
		}else{
			$raw->pics = "";
		}

		if (isset($raw->reco_ids) && is_array($raw->reco_ids) && count($raw->reco_ids) > 0){
			$raw->reco_ids = implode(',', $raw->reco_ids);
		}else{
			$raw->reco_ids = "";
		}
		
		if (isset($raw->topics) && is_array($raw->topics) && count($raw->topics) > 0){
			$raw->tags = 'topic';
			$raw->topic_sign = 1;
		}
		$data = NewsOrigService::save($raw);

		//如果设置了发布时间  2017-11-10 +
		if ($raw->publish_time && $raw->is_display == -1){
			$table_id = NewsOrigService::getTableId();
			$list_id = $data->id;
			$publish_time = $raw->publish_time;
			self::createPublish($table_id, $list_id, $publish_time);
			$delay = strtotime($publish_time) - time();
			$this->queue->put(json_encode([
				"command" => "PUBLISH",
				'table_id' => $table_id,
				'list_id' => $list_id,
				'publish_time' => $publish_time,
				'delay' => $delay
			]));//2017-11-13 +
		}

		//添加专题 2017-10-16 +
		if (isset($raw->topics) && is_array($raw->topics) && count($raw->topics) > 0){
			$topics = $raw->topics;
			
			$topic_id = $data->id;
			//删除专题
			NewsOrigService::deleteTopic($topic_id);
			foreach ($topics as $key => $value) {

				$d['topic_id']= $topic_id;
				$d['program'] = $value->program;
				$d['ids'] 	 = implode(',', $value->id_arr);
				//保存专题
				NewsOrigService::saveTopic($d);
				//已设置为专题的栏目新闻将topic_sing设置为-1
				NewsOrigService::setTopic($value->id_arr);
			}
		}

        //判断是专题那么需要修改其第一个专题的topic_id
        if(isset($raw->topic_id) && $raw->topic_id>0){
            $dataTopic = EwNewsOrig::findFirst($raw->topic_id);
            $dataTopic->topic_id=$raw->topic_id;
            $dataTopic->save();
            //NewsOrigService::save($orgtitle);
        }
        $this->addOperateLogs(array('action'=>'/news-org/create','content'=>'新增原创新闻>'.$raw->title));
		return $this->echoJson('ok');
	}

	public function dtopicAction()
    {
    	$this->getUser();

		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		if (!$raw->id)
			return $this->echoJson();
		$id = $raw->id;
		if (isset($this->user)){
			$data = NewsOrigService::getInfo($id, $this->user->member_id);
		}else{
			$data = NewsOrigService::getInfo($id);
		}
		$arr = [];
		if (is_object($data)){
			//判断缩略图是否有图且url是否完整
			$data->thumb = $data->thumb ? $this->getFullUrl($data->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			$data->pics = explode(',', $data->pics);
			$data->pics = array_filter($data->pics);
			$data->content = $data->content ? $data->content : "";
			$data->go_url = $this->config->gourl . $this->config->urls->news_orig . $data->id;

			$table_id = NewsOrigService::getTableId();
			$list_id = $raw->id;
			$publish = EwPublish::findFirst('table_id =' . $table_id . ' AND list_id =' . $list_id);
			$data->publish_time = "";
			if ($publish)
				$data->publish_time = $publish->publish_time;

			if ($data->topic_sign == 1)
				$data->go_url = $this->config->gourl . $this->config->urls->news_topic . $data->id;
			$data->comment = $data->comment > 10000 ? floor($data->comment / 10000) . '万+' : $data->comment;
			$data->visit = $data->visit > 10000 ? floor($data->visit / 10000) . '万+' : $data->visit;
			$data->sources = $data->sources ? $data->sources : "觅窝";
			$preg = "|(.*)。|isU";
	    	$size = 200;
	    	$data->summary = $data->summary;
			unset($data->is_self);
			unset($data->index_by);
			unset($data->reco_by);
			unset($data->is_display);
			unset($data->is_del);
			unset($data->city_id);
			unset($data->topic_id);
			unset($data->type_id);
			unset($data->order_by);
			unset($data->pics);
			unset($data->content);
			unset($data->is_praise);
			unset($data->praise);
			unset($data->news_type);
			unset($data->sources);
			unset($data->groupId);
			unset($data->create_time);
			unset($data->update_time);
			unset($data->visit);
			unset($data->comment);
			unset($data->visit_re);
		}
		$arr = (array)$data;
		$topics = [];
		if (is_object($data)){
			if ($data->topic_sign == 1){
				$topic_ar = NewsOrigService::getTopic($id);
				if (is_array($topic_ar) && count($topic_ar) > 0){
					foreach ($topic_ar as $kk => $topic) {
						$d = [];
						$news_list = NewsOrigService::getNewsListIds($topic->ids);
						if (count($news_list) <= 0)
							return ;
						foreach ($news_list as $key => $value) {
							unset($value->index_by);
							unset($value->reco_by);
							unset($value->topic);
							$d[$value->id] = $value;

							//判断缩略图是否有图且url是否完整
							$d[$value->id]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

							$d[$value->id]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
							$d[$value->id]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
							$d[$value->id]->sources = $value->sources ? $value->sources : "觅窝";

							if (strtotime($value->create_time) < strtotime(date("Y-m-d", time())) && $value->tags == 'new')
								$value->tags = '';

							$d[$value->id]->create_time = $value->create_time;
							$d[$value->id]->tags = $value->tags ? $value->tags : "";

							$d[$value->id]->is_praise  = (string)self::COLLECT_FALSE;
							$d[$value->id]->is_collect = (string)self::COLLECT_FALSE;
							$is_praise = $d[$value->id]->is_praise ? 1 : 0;
							$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id . '/' . $value->groupId . '/' . $is_praise;
							$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id;
							unset($value->is_praise);
							unset($value->project_name);
							unset($value->is_collect);
							unset($value->praise);
							unset($value->news_type);
							unset($value->sources);
							unset($value->groupId);
							unset($value->create_time);
							unset($value->update_time);
							unset($value->visit);
							unset($value->comment);
							unset($value->is_self);
							unset($value->topic_id);
							unset($value->tag);
							unset($value->topic_group);
							unset($value->type_id);
						}

						$topics[$kk]['program'] = $topic->program;
						$topics[$kk]['id_arr'] = array_values($d);
					}
				}
			}
		}
		$arr['topics'] = array_values($topics);
		return  $this->echoJson($arr);
    }

	/**
	 * [utopicAction 更新]
	 * @return [type] [description]
	 */
	public function utopicAction()
	{
		$user = $this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$raw = $this->validation(new NewsOrgValidation, $this->request->getJsonRawBody());
		$raw->city_id = intval(RolesService::getCityId($user->role_id));
		
        /*
		if (isset($raw->pics) && is_array($raw->pics) && count($raw->pics) > 0){
			$raw->pics = implode(',', $raw->pics);
		}else{
			$raw->pics = "";
		}*/
		if (isset($raw->reco_ids) && is_array($raw->reco_ids) && count($raw->reco_ids) > 0){
			$raw->reco_ids = implode(',', $raw->reco_ids);
		}else{
			$raw->reco_ids = "";
		}

		if (isset($raw->topics) && is_array($raw->topics) && count($raw->topics) > 0){
			$raw->tags = 'topic';
			$raw->topic_sign = 1;
		}

		//添加专题 2017-10-16 +
		if (isset($raw->topics) && is_array($raw->topics) && count($raw->topics) > 0){
			$topics = $raw->topics;
			$topic_id = $raw->id;
			//删除专题
			NewsOrigService::deleteTopic($topic_id);
			foreach ($topics as $key => $value) {
				$d['topic_id']= $topic_id;
				$d['program'] = $value->program;
				$d['ids'] 	 = implode(',', $value->id_arr);
				//保存专题
				NewsOrigService::saveTopic($d);
				//已设置为专题的栏目新闻将topic_sing设置为-1
				NewsOrigService::setTopic($value->id_arr);
			}
		}

        $this->addOperateLogs(array('action'=>'/news-org/update','content'=>'原创新闻编辑ID:'.$raw->id));
        
        //更新不改变创建者
        $old_data = NewsOrigService::getModel($raw->id);
        $raw->creator_id = $old_data->creator_id;

		$data = NewsOrigService::save($raw);

		//如果设置了发布时间  2017-11-10 +
		if ($raw->publish_time && $raw->is_display == -1){
			$table_id = NewsOrigService::getTableId();
			$list_id = $data->id;
			$publish_time = $raw->publish_time;
			self::createPublish($table_id, $list_id, $publish_time);
			$delay = strtotime($publish_time) - time();
			$this->queue->put(json_encode([
				"command" => "PUBLISH",
				'table_id' => $table_id,
				'list_id' => $list_id,
				'publish_time' => $publish_time,
				'delay' => $delay
			]));//2017-11-13 +
		}

		return $this->echoJson('ok');
	}

	/**
	 * [stickAction 置顶]
	 * @return [type] [description]
	 */
	public function stickAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
        NewsOrigService::getStick($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [setcityAction ]
	 * @return [type] [description]
	 */
	public function setcityAction()
	{
		$user = $this->getUser();
		// if($user->role_id != 2)
		// 	return $this->echoJson("非系统管理员，操作被禁！");

		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
		NewsOrigService::setCity($raw->id);
		return $this->echoJson("ok");
	}

	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = NewsOrigService::getModel($raw->id);
		
		$arr = new \stdClass();
		//$go_url = $this->config->gourl . $this->config->urls->news_orig . $data->id;

		if ($data){
			$arr = new \stdClass();
			foreach ($data as $key => $value) {
				$arr->$key = $value;
			}

			$table_id = NewsOrigService::getTableId();
			$list_id = $raw->id;
			$publish = EwPublish::findFirst('table_id =' . $table_id . ' AND list_id =' . $list_id);
			$arr->publish_time = "";
			if ($publish)
				$arr->publish_time = $publish->publish_time;

			if ($data->pics){
				$arr->pics = explode(",", $data->pics);	
			}else{
				$arr->pics = [];
			}
			// $arr->three_url = $data->three_url ? $data->three_url : $go_url;
			// $arr->album = (array)json_decode($data->album);
			$arr->reco_ids = NewsOrigService::getRecoInfo($raw->id);
		}
		
		return $this->echoJson($arr);
	}

	/**
	 * [setrecoAction 执行推荐  将video数据列表推荐到首页-banner]
	 * @return [type] [description]
	 */
	public function setrecoAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
        NewsOrigService::batchReco($raw->id, 2);
		return $this->echoJson("ok");
	}

	/**
	 * [delrecoAction 执行取消推荐]
	 * @return [type] [description]
	 */
	public function delrecoAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
        NewsOrigService::batchReco($raw->id);
		return $this->echoJson("ok");
	}

	/**
	 * [setrecoAction 执行首页 将video数据列表推荐到首页-video]
	 * @return [type] [description]
	 */
	public function setfirstAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
        NewsOrigService::batchReco($raw->id, 1);
		return $this->echoJson("ok");
	}

	/**
	 * [updateAction 更新]
	 * @return [type] [description]
	 */
	public function updateAction()
	{
		$user = $this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$raw = $this->validation(new NewsOrgValidation, $this->request->getJsonRawBody());
		$raw->city_id = intval(RolesService::getCityId($user->role_id));
        /*
		if (isset($raw->pics) && is_array($raw->pics) && count($raw->pics) > 0){
			$raw->pics = implode(',', $raw->pics);
		}else{
			$raw->pics = "";
		}*/
		if (isset($raw->reco_ids) && is_array($raw->reco_ids) && count($raw->reco_ids) > 0){
			$raw->reco_ids = implode(',', $raw->reco_ids);
		}else{
			$raw->reco_ids = "";
		}
        $this->addOperateLogs(array('action'=>'/news-org/update','content'=>'原创新闻编辑ID:'.$raw->id));

        //更新不改变创建者
        $old_data = NewsOrigService::getModel($raw->id);
        $raw->creator_id = $old_data->creator_id;

		$data = NewsOrigService::save($raw);

		//如果设置了发布时间  2017-11-10 +
		if ($raw->publish_time && $raw->is_display == -1){
			$table_id = NewsOrigService::getTableId();
			$list_id = $data->id;
			$publish_time = $raw->publish_time;
			self::createPublish($table_id, $list_id, $publish_time);
			$delay = strtotime($publish_time) - time();
			$this->queue->put(json_encode([
				"command" => "PUBLISH",
				'table_id' => $table_id,
				'list_id' => $list_id,
				'publish_time' => $publish_time,
				'delay' => $delay
			]));//2017-11-13 +
		}

		return $this->echoJson('ok');
	}

	/**
	 * [upAction 上架]
	 * @return [type] [description]
	 */
	public function displayAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = NewsOrigService::display($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [deleteAction 删除]
	 * @return [type] [description]
	 */
	public function deleteAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = NewsOrigService::delete($raw->id);
        $this->addOperateLogs(array('action'=>'/news-org/delete','content'=>'原创新闻删除:'.$raw->id));
        return $this->echoJson('ok');
	}

	/**
	 * [foreverAction 彻底删除]
	 * @return [type] [description]
	 */
	public function foreverAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = NewsOrigService::forever($raw->id);
        $this->addOperateLogs(array('action'=>'/news-org/forever','content'=>'原创新闻删除:'.$raw->id));
		return $this->echoJson('ok');
	}
    /**
     * [foreverAction 彻底删除]
     * @return [type] [description]
     */
    public function newsrecmdsaveAction()
    {
        $user=$this->getUser();

        $para = $this->request->getJsonRawBody();
        $para->unival=md5($para->groupId."".$para->go_url);
        $para->create_date=strtotime("now");
        $para->city_id=$user->roles_info->city_id;
        if(!isset($para->id)  ){//未添加到推荐需要做重复判断
            $exist=NewsOrigService::getNewsOrgRecIspush(" unival='{$para->unival}'");
            if(count($exist)>0){
                return $this->echoJson('不能重复添加',401);
            }
        }
        if(isset($para->thumb) && $para->thumb!='' ){
            ;
        }else{
            return $this->echoJson('缩略图不能为空',401);
        }
        $this->addOperateLogs(array('action'=>'/news-org/newsrecmdsave','content'=>'推荐到app首页:'.$para->title));
        $data = NewsOrigService::saveNewsRecod($para);
        return $this->echoJson('ok');
    }
    /**
     * [foreverAction 彻底删除]
     * @return [type] [description]
     */
    public function newsrecmddetailAction()
    {
        $user=$this->getUser();

        $para = $this->request->getJsonRawBody();
        if(isset($para->id) && intval($para->id)>0 ){//未添加到推荐需要做重复判断
            $exist=NewsOrigService::getNewsOrgRecIspush(" id={$para->id}");
            if(count($exist)>0){
                return $this->echoJson($exist[0]);
            }
        }
        return $this->echoJson('记录不存在',401);
    }
    /**
     * [foreverAction 彻底删除]
     * @return [type] [description]
     */
    public function newsrecmdlistAction()
    {
        $this->getUser();

        $para = $this->request->getJsonRawBody();
        $this->backgroudDateFilterByCityRole($para);

        $data = NewsOrigService::getNewsOrgRecmdList($para);
        $assign = [
            'total' => count($data),
            'list'  => $data
        ];
        return $this->echoJson($assign);
        //return $this->echoJson('ok');
    }
    /**
     * [removeRecmdOrgnewsAction 彻底删除]
     * @return [type] [description]
     */
    public function removeRecmdOrgnewsAction(){
        $user=$this->getUser();
        $para = $this->request->getJsonRawBody();
        if(isset($para->id)){
            $ok=NewsOrigService::removeNewsOrgRecmdRecord($para->id);
        }else{
            $ok=false;
        }
        if($ok){
            return $this->echoJson('ok');
        }else{
            return $this->echoJson('fail');
        }
    }

    public function commentsAction()
    {
    	$this->getUser();
        $raw = $this->request->getJsonRawBody();
        $list = $raw->list;

        if ($list && count($list) > 0){
        	$user_list = $this->getRandomUser(count($list));
        	$i = 0;
        	foreach ($list as $key => $value) {
        		//模拟假评论（同一条新闻两条评论间隔时间5-30m）
        		$delay = rand(60*2,60*30);
        		$this->queue->put(json_encode([
					'command' => 'SIMU_COMMENT',
					'user_id' => $user_list[$i]->id,
					'table_id' => $raw->table_id,
					'list_id' => $raw->list_id,
					'replyer_id' => 0,
					'content' => $value->content,
					'publish_time' => time() + $delay,
					'delay' => $delay
				]));//2017-12-05 +
        	}
        }
        return $this->echoJson('ok');
    }
    /**
     * [makeShamCommentsAction 做假评论]
     * @return [type] [description]
     */
    public function makeShamCommentsAction()
    {
    	$this->getUser();
        $raw = $this->request->getJsonRawBody();
        $list = $raw->list;

        if ($list && count($list) > 0){
        	$user_list = $this->getRandomUser(count($list));
        	$i = 0;
        	foreach ($list as $key => $value) {
        		//模拟假评论（同一条新闻两条评论间隔时间5-30m）
        		$delay = rand(60*2,60*30);
        		if (trim($value->content)){
        			$this->queue->put(json_encode([
						'command' => 'SIMU_COMMENT',
						'user_id' => $user_list[$i]->id,
						'table_id' => $raw->table_id,
						'list_id' => $raw->list_id,
						'replyer_id' => 0,
						'content' => $value->content,
						'publish_time' => time() + $delay,
						'delay' => $delay
					]));//2017-12-05 +
        		}
				$i++;
        	}
        }
  //       $this->getUser();
		// $para = $this->request->getJsonRawBody();
  //       $len=count($para->list);
  //       $userlist=$this->getRandomUser($len);
  //       $i=count($userlist);
  //       $comment_num = count($para->list);
       
  //       foreach($para->list as $key=>$val){
  //           if($i-1<0){
  //               break;
  //           }
  //           $cmt = new \stdClass();
  //           $cmt->table_id=$para->table_id;
  //           $cmt->list_id=$para->list_id;
  //           $cmt->member_id=$userlist[$i-1]->id;
  //           $cmt->content=$val->content;
  //           $cmt->table_id=$para->table_id;
  //           $this->makeShamComments($cmt);
  //           $i--;
  //       }
  //       if($comment_num > 0){
            
  //           $type=trim($para->table_id)."";
  //           $id=intval($para->list_id);
           
  //           if($type=='2'){//评论的表Id   2实时资讯 6视频 10原创新闻
  //               NewsOrigService::updateComments("update ew_news set comment=comment+{$comment_num} where id={$id}");
  //           }elseif($type=='6'){
  //               NewsOrigService::updateComments("update ew_video set comment=comment+{$comment_num} where id={$id}");
  //           }elseif($type=='10'){
  //               NewsOrigService::updateComments("update ew_news_orig set comment=comment+{$comment_num} where id={$id}");
  //           }
  //       }
        return $this->echoJson('ok');
    }
    public function getlogsysAction(){
        $this->getUser();
        $para = $this->request->getJsonRawBody();
        $where= " 1=1";
        if(isset($para->account) && trim($para->account)!=''){
            $account=trim($para->account);
            $where.= " and u.account='{$account}'";
        }
        $sql=" select s.*,u.account from ew_log_sys s LEFT JOIN ew_user u on s.uid=u.id  where {$where} order by s.id desc";
        $assign = $this->excuteSqlQuery($sql,$para);

        return $this->echoJson($assign);

    }

}